package com.zs.zcerp.approval.controller;

import com.zs.zcerp.approval.entity.QhbillstdmEntity;
import com.zs.zcerp.approval.service.IQhBasestdDService;
import com.zs.zcerp.approval.service.IQhbillstdmService;
import com.zs.zcerp.common.annotation.ControllerEndpoint;
import com.zs.zcerp.common.controller.BaseController;
import com.zs.zcerp.common.entity.FebsResponse;
import com.zs.zcerp.common.entity.QueryRequest;
import com.zs.zcerp.common.entity.Strings;
import com.zs.zcerp.common.exception.FebsException;
import com.zs.zcerp.entrance.entity.Syorg;
import com.zs.zcerp.entrance.service.ISyorgService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.util.List;


/**
 * 分类(类别)Controller
 *
 * @author Henry
 * @email *****@mail.com
 * @date 2021-04-20 20:58:44
 */
@Slf4j
@Validated
@RestController
@RequiredArgsConstructor
@RequestMapping("classify")
public class QhbillstdmController extends BaseController {

    private final IQhbillstdmService iQhbillstdmService;
    private final ISyorgService iSyorgService;

    @GetMapping("list")
    @RequiresPermissions("classify:view")
    public FebsResponse classifyList(QhbillstdmEntity qhbillstdmEntity, QueryRequest request) {
        if(qhbillstdmEntity.getBillstdNo()!=null){
            qhbillstdmEntity.setBillstdNo(qhbillstdmEntity.getBillstdNo().trim());
        }
        if(qhbillstdmEntity.getBillstdNm()!=null){
            qhbillstdmEntity.setBillstdNm(qhbillstdmEntity.getBillstdNm().trim());
        }
        return new FebsResponse().success()
                .data(getDataTable(iQhbillstdmService.findClassifyDetailList(qhbillstdmEntity, request)));
    }

    /**
     *   已经不用 保留着是否其他地方 调用
     * @param qhbillstdmEntity
     * @param request
     * @return
     */
   // @GetMapping("getList")
    public FebsResponse getList(QhbillstdmEntity qhbillstdmEntity, QueryRequest request) {
        if(qhbillstdmEntity.getBillstdNo()!=null){
            qhbillstdmEntity.setBillstdNo(qhbillstdmEntity.getBillstdNo().trim());
        }
        if(qhbillstdmEntity.getBillstdNm()!=null){
            qhbillstdmEntity.setBillstdNm(qhbillstdmEntity.getBillstdNm().trim());
        }
        return new FebsResponse().success()
                .data(getDataTable(iQhbillstdmService.findClassifyDetailList(qhbillstdmEntity, request)));
    }

    /***
     * 获取多有机构列表
     * 多地方调用不设置权限  勿可修改 删除
     * @return
     */
    @GetMapping("getOrgList")
    public FebsResponse getOrgList() {
        List<Syorg> syorgList = iSyorgService.listNotSyorgs();

        return new FebsResponse().success()
                .data(syorgList);
    }






    @PostMapping("add")
    @RequiresPermissions("classify:add")
    @ControllerEndpoint(operation = "新增分类", exceptionMessage = "新增分类失败:分类编号已经存在")
    public FebsResponse addClassify(@Valid QhbillstdmEntity qhbillstdmEntity) {
        iQhbillstdmService.createBase(qhbillstdmEntity);
        return new FebsResponse().success();
    }


    @PostMapping("update")
    @RequiresPermissions("classify:update")
    @ControllerEndpoint(operation = "修改分类", exceptionMessage = "修改分类失败")
    public FebsResponse updateClassify(@Valid QhbillstdmEntity qhbillstdmEntity) {
        if (qhbillstdmEntity.getBillstdNo() == null) {
            throw new FebsException("分类编码不能空");
        }
        iQhbillstdmService.updateBase(qhbillstdmEntity);
        return new FebsResponse().success();
    }

    @GetMapping("delete/{billstdNos}")
    @RequiresPermissions("classify:delete")
    @ControllerEndpoint(operation = "删除分类", exceptionMessage = "删除分类失败")
    public FebsResponse deleteClassify(@NotBlank(message = "{required}") @PathVariable String billstdNos) {
        iQhbillstdmService.deleteBaseByBasestdNo(StringUtils.split(billstdNos, Strings.COMMA));
        return new FebsResponse().success();
    }

 
    @GetMapping("{billstdNo}")
    public QhbillstdmEntity getClassify(@NotBlank(message = "{required}") @PathVariable String billstdNo) {
        return iQhbillstdmService.findClassifyDetailList(billstdNo);
    }




}
